define(function( require ) {
    var Backbone = require('backbone');

    var ImageModel = require('components/image/1.0.0/models/image');

    var utils = require('core/utils');
    var url;
    if (utils.isFenxiao()) {
        url = _global.url.fenxiao + '/showcase/attachment/alert?media_type=1&v=2';
    } else {
        url = _global.url.www + '/showcase/attachment/alert?media_type=1&v=2';
    }
    return Backbone.Collection.extend({
        url: url,

        model: ImageModel,

        _pageNumber: 1,

        parse: function(resp) {
            if (resp.errcode == 0) {
                this.pagenavi = resp.data.page_view;
                return resp.data.data_list;
            }
            utils.errorNotify(resp.errmsg || '出错啦。');
        },

        _selectedModel: [],

        _idAttribute: 'attachment_id',

        getSelectedImages: function() {
            return this._selectedModel;
        },

        select: function(model) {
            var that = this;

            if (model.get('attachment_size') > this.maxSize * 1024) {
                utils.errorNotify('图片不能大于 ' + this.maxSize + ' KB');
                return false;
            }

            var query = {};
            query[this._idAttribute] = model.get(this._idAttribute);

            var result = _.where(this._selectedModel, query);
            if (result.length > 0) {
                _.each(result, function(item) {
                    that._selectedModel.splice(that._selectedModel.indexOf(item), 1);
                });
            } else {
                this._selectedModel.push(model.attributes);
            }

            this.trigger('select', this._selectedModel.length, model);
            return true;
        },

        isSelected: function(model) {
            var query = {};
            query[this._idAttribute] = model.get(this._idAttribute);

            var result = _.where(this._selectedModel, query);
            if (result.length > 0) {
                return true;
            }
            return false;
        },

        clearAllSelected: function() {
            this._selectedModel.length = 0;
            this.trigger('clear');
            this.trigger('select', 0);
        }
    });
});